package cn.schoolwow.ams.test.config;

import cn.schoolwow.ams.test.config.flow.TestCaseQuickFlowTryCatchFinally;
import cn.schoolwow.ams.test.config.flow.TestFlowMappingHandler;
import cn.schoolwow.ams.test.config.flow.TestQuickFlowTryCatchFinally;
import cn.schoolwow.quickflow.QuickFlow;
import cn.schoolwow.quickflow.QuickFlowBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class QuickFlowConfig {
    @Bean
    @Autowired
    public QuickFlow quickFlow(ApplicationContext applicationContext) {
        QuickFlow quickFlow = QuickFlowBuilder.newInstance()
                .tryCatchFinallyHandler(applicationContext.getBean(TestQuickFlowTryCatchFinally.class))
                .flowNameInstanceMappingHandler(applicationContext.getBean(TestFlowMappingHandler.class))
                .putContextTemporaryInstanceData(applicationContext, ApplicationContext.class)
                .putContextTemporaryInstanceData(applicationContext.getEnvironment(), Environment.class);
        return quickFlow;
    }

    @Bean
    @Autowired
    public QuickFlow testFlow(ApplicationContext applicationContext) {
        QuickFlow testFlow = QuickFlowBuilder.newInstance()
                .flowNameInstanceMappingHandler(applicationContext.getBean(TestFlowMappingHandler.class))
                .putContextTemporaryInstanceData(applicationContext, ApplicationContext.class)
                .putContextTemporaryInstanceData(applicationContext.getEnvironment(), Environment.class);
        return testFlow;
    }

    @Bean
    @Autowired
    public QuickFlow testcaseFlow(ApplicationContext applicationContext) {
        QuickFlow testFlow = QuickFlowBuilder.newInstance()
                .tryCatchFinallyHandler(applicationContext.getBean(TestCaseQuickFlowTryCatchFinally.class))
                .flowNameInstanceMappingHandler(applicationContext.getBean(TestFlowMappingHandler.class))
                .putContextTemporaryInstanceData(applicationContext, ApplicationContext.class)
                .putContextTemporaryInstanceData(applicationContext.getEnvironment(), Environment.class);
        return testFlow;
    }

}
